//获取应用实例
var app = getApp(),
    core = app.requirejs("core");

Page({
    data: {
        "card_info": {},
        "card_code_img": ""
    },

    // 监听页面加载
    onLoad: function(options) {
        // 防止有特殊符号报错
        var card_info = JSON.parse(decodeURIComponent(options.card_info));
        console.log("qr_card_param", card_info);
        this.setData({
            card_info: card_info
        })
        console.log("opt",card_info)

        this.getCardCode();
    },

    // 获取名片码
    getCardCode: function() {
        var that = this;

        wx.showToast({
            title: '正在获取',
            icon: 'loading',
            duration: 10000
        })

        core.get('qr/code', {
            path: 'yb_mingpian/pages/cardinfo/index',
            scene: that.data.card_info.card_id,
            base64: 1
        }, function(res) {
            //var base64 = wx.arrayBufferToBase64(res.data);
            if (res.code == 0) {
                that.setData({
                    card_code_img: "data:image/png;base64," + res.info
                })
                wx.hideToast();
            } else {
                core.alert('获取失败');
            }
        })
    },

    // 分享名片
    onShareAppMessage: function() {
        app.userlog(2, '名片', 'card_relay');
        var title = this.data.card_info.share_title;

        if (title == '' || title == null) {
            if (this.data.card_info.company_name){
                title = '你好,我是' + this.data.card_info.company_name + '的' + this.data.card_info.user_name + ',这是我的名片请惠存';
            } else {
                title = '你好,我是' + this.data.card_info.user_name + ',这是我的名片请惠存';
            }
        }

        var uid = app.getCache('userinfo').uid ? app.getCache('userinfo').uid : 0;
        var path = 'yb_mingpian/pages/cardinfo/index?source=3&id=' + app.globalData.card_id + '&pid=' + uid;

        return {
            title: title,
            path: path,
        }
    },

    // 生成名片码
    to_poster: function() {
        var avatar = this.data.card_info.head_photo ? this.data.card_info.head_photo : '';
        core.jump('/yb_mingpian/pages/card_poster/index?card_id=' + app.globalData.card_id + '&avatar=' + avatar + '&user_name=' + this.data.card_info.user_name + '&position=' + this.data.card_info.position + '&share_type=card', 1);
    },

})